package com.it.reggie.Service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.it.reggie.Mapper.CategoryMapper;
import com.it.reggie.Mapper.DishMapper;
import com.it.reggie.Mapper.SetmealMapper;
import com.it.reggie.Service.CategoryService;
import com.it.reggie.common.CustomException;
import com.it.reggie.entity.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;


    /**
     * 员工总条数
     * @return
     */
    @Override
    public int selectTotalCount() {
        int i = categoryMapper.selectTotalCount();
        return i;
    }

    /**
     * 根据id删除分类 ，删除之前要进行判断
     * @param id
     */
    @Override
    public void remove(Long id) {

        //查询当前分类是否关联菜品，如果关联抛出业务异常
        int i = dishMapper.queryDishes(id);
        if (i > 0){
            throw new CustomException("当前分类下关联菜品，不能删除");
        }

        //查询当前分类是否关联套餐，如果关联抛出业务异常
        int i1 = setmealMapper.querysetMeal(id);
        if (i1>0){
            throw new CustomException("当前分类下关联套餐，不能删除");
        }

        //删除业务
        super.removeById(id);


    }

    @Override
    public List<Category> listText(int type) {
        List<Category> list = categoryMapper.list(type);
        return list;
    }


    /**
     * 查询全部分类
     * @return
     */
    @Override
    public List<Category> listAll() {
        List<Category> categories = categoryMapper.listAll();
        return categories;
    }
}
